#include <iostream>
#include <cstdio>
using namespace std;
int main() {
    int p,e,k,d;
    int key;
    int cas=1;
    while (1) {
        scanf ("%d%d%d%d",&p,&e,&k,&d);
        if (p==-1) break;
        key=23;
        int i;
        for (i=p; ; i=i+key) {
            if ((i-e)%28==0) key=23*28;
            if ((i-e)%28==0 && (i-k)%33==0 && i!=0) break;
        }
        printf ("Case %d: the next triple peak occurs in %d days.\n", cas, i-d);
        cas++;
    }
    return 0;
}
